<?php
namespace Home\Controller;

class FollowController extends \Home\Controller\BaseController
{
	/**
	 * 获取产品关注列表
	 * @author 刘新
	 */
	public function followList()
	{
		$param = I('params');
		$where =  array('uf.user_id'=>$this->user['userId'], 'uf.status'=>1, 'f.status'=>1);
		$field = 'uf.user_id,uf.fund_id,f.fund_name,f.trader_name';
		$page = $param['page'] ? $param['page'] : 1;
		$size = $param['size'] ? $param['size'] : 1;
		$userFund = M('UserFund');
		$list = $userFund->alias('uf')->join('__FUND__ f on uf.fund_id=f.id')->where($where)->page($page, $size)->field($field)->select();
	
		//查询失败
		if($list === false) {
			$this->errorLog(__FILE__, __LINE__, $userFund->getDbError() . '---' . $userFund->getError());
			$this->ajaxReturn(array('error' => 1, 'info' => '查询失败'));
		}
	
		//查询成功
		$return = $this->successResult($list);
		$this->ajaxReturn($return);
	}
	
	/**
	 * 添加产品关注
	 * @author 刘新
	 */
	public function addFollow()
	{
		$param = I('params');
		$data['user_id'] = $this->user['userId'];
		$data['fund_id'] = $param['fund_id'];
	
		//查询是否有关注过
		$fo = $this->returnOne('UserFund', $data, '', 'id');
		if($fo) $this->ajaxReturn(array('error' => 1, 'info' => '已关注过'));
	
		//添加关注
		$res = D('UserFund')->addFollow($data);
	
		//关注成功
		if($res['error'] === 0) $this->ajaxReturn(array('error' => 0, 'info' => '关注成功'));
	
		//关注失败，记录日志
		$res = $this->errorLog(__FILE__, __LINE__, $res['info']);
		$this->ajaxReturn(array('error' => 1, 'info' => '关注失败'));
	}
	
	/**
	 * 取消产品关注
	 * @author 刘新
	 */
	public function cancelFollow()
	{
		$param = I('params');
		$data['id'] = $param['id'];
	
		//查询是否有关注过
		$fo = $this->returnOne('UserFund', $data, '', 'id');
		if(!$fo) $this->ajaxReturn(array('error' => 1, 'info' => '您没有关注该产品'));
	
		//取消关注
		$data['status'] = -1;
		$res = D('UserFund')->editFollow($data);
		if($res['error'] === 0) $this->ajaxReturn(array('error' => 0, 'info' => '取消关注成功'));
	
		//取消关注失败
		$res = $this->errorLog(__FILE__, __LINE__, $res['info']);
		$this->ajaxReturn(array('error' => 1, 'info' => '取消关注失败'));
	}
}